package com.acwing.partition2;

import java.io.*;
import java.util.*;

/**
 * 索引位置表
 *        0     1
 *        2     3
 *  4  5  6  7  8  9  10
 *        11    12
 *  13 14 15 16 17 18 19
 *        20    21
 *        22    23
 * @author `RKC`
 * @date 2022/1/5 17:43
 */
public class AC181回转游戏aStar {

    private static final int N = 24, P = 131;
    private static int[] nums = new int[N];
    //operation[i][j]表示i操作，在i操作中的下标j实际在nums中的位置
    private static int[][] operation = {{0, 2, 6, 11, 15, 20, 22}, {1, 3, 8, 12, 17, 21, 23},
            {10, 9, 8, 7, 6, 5, 4}, {19, 18, 17, 16, 15, 14, 13}, {23, 21, 17, 12, 8, 3, 1},
            {22, 20, 15, 11, 6, 2, 0}, {13, 14, 15, 16, 17, 18, 19}, {4, 5, 6, 7, 8, 9, 10}};
    //表示中间8个元素在nums中的索引
    private static int[] center = {6, 7, 8, 11, 12, 15, 16, 17};
    //逆操作的对应索引
    private static int[] opposite = {5, 4, 7, 6, 1, 0, 3, 2};

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            String[] s = reader.readLine().split("\\s+");
            if (s.length == 1 && Integer.parseInt(s[0]) == 0) break;
            for (int i = 0; i < s.length; i++) nums[i] = Integer.parseInt(s[i]);
            Node answer = aStar(nums);
            if (answer.dist == 0) writer.write("No moves needed\n");
            else {
                for (int i = 0; i < answer.operations.size(); i++) writer.write(answer.operations.get(i));
                writer.write("\n");
            }
            //随便输出一个在中间的数字
            writer.write(answer.nums[center[0]] + "\n");
        }
        writer.flush();
    }

    private static Node aStar(int[] nums) {
        Map<Long, Integer> distMap = new HashMap<>();
        PriorityQueue<Node> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.val, o2.val));
        long h = numsHash(nums);
        distMap.put(h, 0);
        heap.add(new Node(eval(nums), nums, h, -1, 0));
        while (!heap.isEmpty()) {
            Node curr = heap.poll();
            int dist = distMap.get(curr.hashKey);
            //得到的字典序并不是最小的，如果要得到最小的字典序，需要额外的处理，或者直接使用IDA*算法
            if (eval(curr.nums) == 0) return curr;
            for (int i = 0; i < operation.length; i++) {
                //不进行逆操作
                if (curr.last != opposite[i]) {
                    int[] next = operate(curr.nums, i);
                    h = numsHash(next);
                    if (distMap.containsKey(h)) continue;
                    Node nextNode = new Node(eval(next) + 1 + dist, next, h, i, dist + 1);
                    nextNode.operations.addAll(curr.operations);
                    nextNode.operations.add((char) ('A' + i));
                    heap.add(nextNode);
                    distMap.put(h, dist + 1);
                }
            }
        }
        return null;
    }

    private static int[] operate(int[] nums, int x) {
        int[] result = new int[nums.length];
        System.arraycopy(nums, 0, result, 0, nums.length);
        int temp = result[operation[x][0]];
        for (int i = 0; i < 6; i++) result[operation[x][i]] = result[operation[x][i + 1]];
        result[operation[x][6]] = temp;
        return result;
    }

    private static int eval(int[] nums) {
        //找到中间8个数出现次数最多的数字
        int[] count = new int[4];
        for (int i = 0; i < center.length; i++) count[nums[center[i]]]++;
        int num = 0;
        for (int i = 1; i <= 3; i++) num = Math.max(num, count[i]);
        return 8 - num;
    }

    private static long numsHash(int[] nums) {
        long[] hash = new long[nums.length + 1];
        for (int i = 1; i <= nums.length; i++) hash[i] = hash[i - 1] * P + nums[i - 1];
        return hash[nums.length];
    }

    private static class Node {
        private int val;
        private int[] nums;
        private long hashKey;
        private int last;
        private List<Character> operations = new ArrayList<>(20);
        private int dist;

        public Node(int val, int[] nums, long hashKey, int last, int dist) {
            this.val = val;
            this.nums = nums;
            this.hashKey = hashKey;
            this.last = last;
            this.dist = dist;
        }
    }
}
